﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Confighelper
{
    public class FileHelper
    {
        public static List<String> GetAllKeys(string config)
        {
            List<String> keys = new List<string>();
            string pattern = "key=\"(.*?)\"";
            MatchCollection s = Regex.Matches(config, pattern);
            for(int index = 0; index < s.Count; index++)
            {
                keys.Add(s[index].Groups[1].Value);
            }
            return keys;
        }

        public static void Serialize<T>(T t)
        {
            XmlSerializer seri = new XmlSerializer(typeof(T));
            using (FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + @"Configs.xml",FileMode.Create))
            {
                seri.Serialize(fs,t);
            }
        }

        public static T Deserialize<T>(string filePath) where T:class
        {
            XmlSerializer seri = new XmlSerializer(typeof(T));
            using (FileStream fs = new FileStream(filePath, FileMode.Open))
            {
               return (T)seri.Deserialize(fs);
            }
            
        }

    }
}
